package com.handpay.nocardpay.baselibrary.main.mine.view.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.account.model.bean.VerificationCodeBean;
import com.handpay.nocardpay.baselibrary.account.presenter.ModifyUserinfoPresenterImpl;
import com.handpay.nocardpay.baselibrary.account.presenter.VerfcodePresenterImpl;
import com.handpay.nocardpay.baselibrary.account.view.ISendVerfCodeView;
import com.handpay.nocardpay.baselibrary.account.view.ImodifyUserInfoView;
import com.handpay.nocardpay.baselibrary.account.view.activity.LoginActivity;
import com.handpay.nocardpay.baselibrary.commbase.BaseProtocolKey;
import com.handpay.nocardpay.baselibrary.commbase.view.fragment.BaseFragment;
import com.handpay.nocardpay.baselibrary.config.NoCardPayConfig;
import com.handpay.nocardpay.baselibrary.config.SecureManager;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.utils.AccountUtils;
import com.handpay.nocardpay.baselibrary.utils.ClientEngine;
import com.handpay.nocardpay.baselibrary.utils.CommonUtils;
import com.handpay.nocardpay.baselibrary.utils.DialogUtils;
import com.handpay.nocardpay.baselibrary.utils.NoCardPayConstant;

import java.util.HashMap;

/**
 * Created by sxshi on 2017/8/14.
 */

public class ModifyPhoneFragment extends BaseFragment implements ISendVerfCodeView, ImodifyUserInfoView {
    private AppCompatEditText et_phone, et_code;
    private TextView mGetVerfcode;
    private Button btn_submit;
    private CountDownTimer mCountDownTimer;
    private VerfcodePresenterImpl verfcodePresenter;
    private ModifyUserinfoPresenterImpl modifyUserinfoPresenter;
    private HashMap<String, Object> paramers = new HashMap<>();

    private String newPhone;
    private String oldPhone;

    public static ModifyPhoneFragment newInstance(String phoneNumber) {

        Bundle args = new Bundle();
        args.putString(NoCardPayConstant.AUTHCODE_ACTION_OLDPHONE, phoneNumber);
        ModifyPhoneFragment fragment = new ModifyPhoneFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_modify_phone;
    }

    @Override
    public void initView(View view) {
        Toolbar mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
        initToolbar(mToolbar, true);
        et_phone = (AppCompatEditText) view.findViewById(R.id.et_phone);
        et_code = (AppCompatEditText) view.findViewById(R.id.et_code);
        mGetVerfcode = (TextView) view.findViewById(R.id.btn_getcode);
        btn_submit = (Button) view.findViewById(R.id.btn_submit);
    }

    @Override
    public void initListener() {
        mGetVerfcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                newPhone = et_phone.getText().toString();
                if (TextUtils.isEmpty(newPhone)) {
                    DialogUtils.showToast(_mActivity, _mActivity.getString(R.string.hint_no_card_pay_please_input_phone_no));
                    return;
                }
                if (!CommonUtils.isMobileNum(newPhone)) {
                    DialogUtils.showToast(_mActivity, _mActivity.getString(R.string.input_right_phone_tip));
                    return;
                }

                mGetVerfcode.setEnabled(false);
                mCountDownTimer.start();
                sendVerCode();
            }
        });
        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String verifyCode = et_code.getText().toString().trim();
                newPhone = et_phone.getText().toString();
                if (TextUtils.isEmpty(newPhone)) {
                    DialogUtils.showToast(_mActivity, _mActivity.getString(R.string.hint_no_card_pay_please_input_phone_no));
                    return;
                }
                if (!CommonUtils.isMobileNum(newPhone)) {
                    DialogUtils.showToast(_mActivity, _mActivity.getString(R.string.input_right_phone_tip));
                    return;
                }
                if (newPhone.equals(oldPhone)) {
                    DialogUtils.showToast(_mActivity, _mActivity.getString(R.string.original_mobile_or_new_phone));
                    return;
                }
                if (TextUtils.isEmpty(verifyCode)) {
                    DialogUtils.showToast(_mActivity, _mActivity.getString(R.string.verfcode));
                    return;
                }
                paramers.put(BaseProtocolKey.PHONE, SecureManager.getInstance().des(oldPhone, 1, null));
                paramers.put(BaseProtocolKey.ACCOUNT, SecureManager.getInstance().des(AccountUtils.getAccountName(_mActivity), 1, null));
                paramers.put(BaseProtocolKey.ACTION, NoCardPayConstant.AUTHCODE_ACTION_NEWPHONE);
                paramers.put(BaseProtocolKey.NEWPHONE, SecureManager.getInstance().des(newPhone, 1, null));
                paramers.put(BaseProtocolKey.CHANNEL, ClientEngine.getInstance().CHANNEL);
                paramers.put(BaseProtocolKey.VERIFYCODE, verifyCode);
                modifyUserinfoPresenter.modifyPhoneNumber(paramers, true);
            }
        });
    }

    /**
     * 发送验证码
     */
    private void sendVerCode() {
        HashMap<String, Object> param = new HashMap<String, Object>();
        param.put(BaseProtocolKey.ACCOUNT, SecureManager.getInstance().des(newPhone, 1, null));
        param.put(BaseProtocolKey.ACTION, NoCardPayConstant.AUTHCODE_ACTION_NEWPHONE);
        param.put(BaseProtocolKey.NEWPHONE, SecureManager.getInstance().des(newPhone, 1, null));
        if (NoCardPayConfig.isNoCardPay) {
            param.put(BaseProtocolKey.CHANNEL, ClientEngine.getInstance().CHANNEL);
        } else {
            param.put(BaseProtocolKey.CHANNEL, ClientEngine.getInstance().FASTBILLCHANNEL);
        }
        verfcodePresenter.getVerificationCode(param, VerificationCodeBean.class, false);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        verfcodePresenter = new VerfcodePresenterImpl(_mActivity);
        verfcodePresenter.setSendVerfCodeView(this);

        modifyUserinfoPresenter = new ModifyUserinfoPresenterImpl(_mActivity);
        modifyUserinfoPresenter.setUserInfoView(this);

        oldPhone = getArguments().getString(NoCardPayConstant.AUTHCODE_ACTION_OLDPHONE);

        mCountDownTimer = new CountDownTimer(NoCardPayConstant.MILLISINFUTURE, NoCardPayConstant.COUNTDOWNINTERVAL) {
            @Override
            public void onTick(long millisUntilFinished) {
                mGetVerfcode.setText((millisUntilFinished / 1000) + _mActivity.getString(R.string.letter_getVefCode));
            }

            @Override
            public void onFinish() {
                mGetVerfcode.setEnabled(true);
                mGetVerfcode.setText(_mActivity.getString(R.string.getvercode));
            }
        };
    }

    @Override
    public void modifyUserInfoFinsh() {
        ClientEngine.getInstance().saveRMSData(BaseProtocolKey.NOCARDPAY_NAME, newPhone);
        DialogUtils.showAlertDialog(_mActivity, _mActivity.getString(R.string.tip), _mActivity.getString(R.string.modifily_phone_success), false, _mActivity.getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                goLogin();
            }
        });
    }

    /**
     * 登录
     */
    private void goLogin() {
        Intent intent = new Intent();
        intent.setClass(_mActivity, LoginActivity.class);
        intent.putExtra(BaseProtocolKey.LOGIN_TYPE, NoCardPayConfig.isNoCardPay);
        _mActivity.startActivity(intent);
        _mActivity.finish();
    }

    @Override
    public void showSendVerfCodeSuccess() {
        DialogUtils.showToast(_mActivity, _mActivity.getString(R.string.send_verfcode_success));
    }

    @Override
    public String getFragmentTitle() {
        return _mActivity.getString(R.string.modify_phone);
    }

}
